package com.zhihaihe.file.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhihaihe.file.annotation.Auth;
import com.zhihaihe.file.dto.announcement.QueryDTO;
import com.zhihaihe.file.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhihaihe.file.pojo.AnnouncementEntity;
import com.zhihaihe.file.service.AnnouncementService;



/**
 * 公告表
 *
 * @author wjx
 * @email 
 * @date 2021-11-05 17:18:21
 */
@RestController
@RequestMapping("announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    /**
     * 前台列表
     */
    @RequestMapping("front/list")
    public R list(){
        LambdaQueryWrapper<AnnouncementEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementEntity::getIfcancel,"0");
        wrapper.orderByAsc(AnnouncementEntity::getPriority);
        wrapper.orderByDesc(AnnouncementEntity::getPublishtime);
        List<AnnouncementEntity> list = announcementService.list(wrapper);
        return R.success("查询成功",list);
    }

    /**
     * 后台列表
     */
    @Auth
    @RequestMapping("/list")
    public R list(Page params , QueryDTO queryDTO){
        Page page =announcementService.likePage(params,queryDTO);
        return R.success("查询成功",page);
    }


    /**
     * 信息
     */
    @Auth
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		AnnouncementEntity announcement = announcementService.getById(id);
        return R.success("查询成功",announcement);
    }

    /**
     * 保存
     */
    @Auth
    @RequestMapping("/save")
    public R save(@RequestBody AnnouncementEntity announcement){
        announcement.setPublishtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		announcementService.save(announcement);
        return R.success("保存成功");
    }

    /**
     * 修改
     */
    @Auth
    @RequestMapping("/update")
    public R update(@RequestBody AnnouncementEntity announcement){
		announcementService.updateById(announcement);
        return R.success("修改成功");
    }

    /**
     * 删除
     */
    @Auth
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		announcementService.removeByIds(Arrays.asList(ids));
        return R.success("删除成功");
    }

}
